﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class ChanpinList : BasePage
    {
        private ExcuteDao proManage = new ExcuteDao();
        private ProductFilter product = new ProductFilter();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["UrlType"] != null && Request.QueryString["UrlType"].Equals("menu"))
            {
                Session["SortExpression_proList"] = null;
                Session["SortDirection_proList"] = null;

                Session["drpIssuanceAgency"] = null;
                Session["drpClass"] = null;
                Session["drpInvestmentIndustry"] = null;
                Session["drpExpectedReturnOnTime"] = null;
                Session["drpDeadline"] = null;
                Session["drpState"] = null;
                Session["drpTZMK"] = null;
                Session["txt_Key"] = null;
            }

            if (Session["UrlType"] != null)
            {
                if (Session["UrlType"].Equals("Editpro"))
                {
                    setSessionData();
                    if (Session["proListIndex"] != null && string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
                    {
                        gvproList.PageIndex = (int)Session["proListIndex"];
                    }

                    Session["UrlType"] = null;
                }
            }
            if (!string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                string pageIndex = Request.QueryString["pageIndex"];
                int index = Convert.ToInt32(pageIndex);
                gvproList.PageIndex = index;

                Session["proListIndex"] = index;

                setSessionData();
            }
            //设置分页状态
            if (Session["SortExpression_proList"] != null)
            {
                this.gvproList.Attributes["SortExpression_proList"] = Session["SortExpression_proList"].ToString();
                this.gvproList.Attributes["SortDirection_proList"] = Session["SortDirection_proList"].ToString();
                setSortColumnStyle(this.gvproList, Session["SortExpression_proList"].ToString(), Session["SortDirection_proList"].ToString());
            }

            if (!IsPostBack)
            {
                ViewState["proAll"] = proManage.Get_ProductAll(drpState.SelectedItem.Text,
                                                drpIssuanceAgency.SelectedItem.Text,
                                                drpDeadline.SelectedValue,
                                                drpExpectedReturnOnTime.SelectedValue,
                                                drpTZMK.SelectedValue,
                                                drpInvestmentIndustry.SelectedItem.Text,
                                                drpClass.SelectedItem.Text,
                                                SpecialCharHandler.HtmlEncode(txt_Key.Text),"");

                SearchproBindData();
            }
        }

        private void setSessionData()
        {
            //检索条件记录

            if (Session["drpIssuanceAgency"] != null)
            {
                drpIssuanceAgency.Text = Session["drpIssuanceAgency"].ToString();
            }

            if (Session["drpClass"] != null)
            {
                drpClass.Text = Session["drpClass"].ToString();
            }

            if (Session["drpInvestmentIndustry"] != null)
            {
                drpInvestmentIndustry.Text = Session["drpInvestmentIndustry"].ToString();
            }

            if (Session["drpExpectedReturnOnTime"] != null)
            {
                drpExpectedReturnOnTime.Text = Session["drpExpectedReturnOnTime"].ToString();
            }

            if (Session["drpDeadline"] != null)
            {
                drpDeadline.Text = Session["drpDeadline"].ToString();
            }

            if (Session["drpState"] != null)
            {
                drpState.Text = Session["drpState"].ToString();
            }

            if (Session["drpTZMK"] != null)
            {
                drpTZMK.Text = Session["drpTZMK"].ToString();
            }

            if (Session["txt_Key"] != null)
            {
                txt_Key.Text = SpecialCharHandler.HtmlDiscode(Session["txt_Key"].ToString());
            }

            if (Session["proListIndex"] != null)
            {
                gvproList.PageIndex = (int)Session["proListIndex"];
            }
        }
        #region 信息列表事件

        /// <summary>
        /// 信息列表数据加载时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvproList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("id", e.Row.RowIndex.ToString());
                e.Row.Attributes.Add("onKeyDown", "SelectRow();");
                e.Row.Attributes.Add("onClick", "MarkRow(" + e.Row.RowIndex.ToString() + ");");
                ((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "../Customer/EditChanpin.aspx?CPID="
                    + SpecialCharHandler.HtmlDiscode(((HyperLink)e.Row.Cells[0].Controls[0]).Text).Replace("&", "amp;");

                //e.Row.Cells[1].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[1].Text);

                string cpname = SpecialCharHandler.HtmlDiscode(e.Row.Cells[1].Text);
                e.Row.Cells[1].ToolTip = cpname;
                if (cpname.Length > 15)
                {
                    cpname = cpname.Substring(0, 15) + "...";
                }
                e.Row.Cells[1].Text = cpname;

                e.Row.Cells[5].Text = e.Row.Cells[5].Text + "万元";
                e.Row.Cells[6].Text = e.Row.Cells[6].Text + "个月";

            }
        }
        #endregion

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState.Clear();
            ViewState["proAll"] = proManage.Get_ProductAll(drpState.SelectedItem.Text,
                                                drpIssuanceAgency.SelectedItem.Text,
                                                drpDeadline.SelectedValue,
                                                drpExpectedReturnOnTime.SelectedValue,
                                                drpTZMK.SelectedValue,
                                                drpInvestmentIndustry.SelectedItem.Text,
                                                drpClass.SelectedItem.Text,
                                                SpecialCharHandler.HtmlEncode(txt_Key.Text), "");
            //清空分页状态
            Session["SortExpression_proList"] = null;
            Session["SortDirection_proList"] = null;

            //检索条件记录
            Session["drpIssuanceAgency"] = drpIssuanceAgency.Text;
            Session["drpClass"] = drpClass.Text;
            Session["drpInvestmentIndustry"] = drpInvestmentIndustry.Text;
            Session["drpExpectedReturnOnTime"] = drpExpectedReturnOnTime.Text;
            Session["drpDeadline"] = drpDeadline.Text;
            Session["drpState"] = drpState.Text;
            Session["drpTZMK"] = drpTZMK.Text;
            Session["txt_Key"] = SpecialCharHandler.HtmlEncode(txt_Key.Text);

            SearchproBindData();

        }

        /// <summary>
        /// 选择页面功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvproList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvproList.PageIndex = e.NewPageIndex;
            pageIndex = e.NewPageIndex;
            Session["proListIndex"] = pageIndex;
            SearchproBindData();
        }

        /// <summary>
        /// GridView排序事件
        /// </summary>
        protected void gvproList_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string SortExpression_proList = e.SortExpression.ToString();
            //// 假定为排序方向为“顺序”
            string SortDirection_proList = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (SortExpression_proList == this.gvproList.Attributes["SortExpression_proList"])
            {
                //获得下一次的排序状态
                SortDirection_proList = (this.gvproList.Attributes["SortDirection_proList"].ToString() == SortDirection_proList ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.gvproList.Attributes["SortExpression_proList"] = SortExpression_proList;
            this.gvproList.Attributes["SortDirection_proList"] = SortDirection_proList;
            //记录分页状态
            Session["SortExpression_proList"] = SortExpression_proList;
            Session["SortDirection_proList"] = SortDirection_proList;

            setSortColumnStyle(this.gvproList, SortExpression_proList, SortDirection_proList);
            this.SearchproBindData();
        }

        /// <summary>
        /// 绑定到GridView
        /// </summary>
        private void SearchproBindData()
        {
            // 获取GridView排序数据列及排序方向
            string SortExpression_proList = this.gvproList.Attributes["SortExpression_proList"];
            string SortDirection_proList = this.gvproList.Attributes["SortDirection_proList"];
            // 调用业务数据获取方法
            DataTable dtBind = (DataTable)ViewState["proAll"];

            //页面数据总数
            recordCount = dtBind.Rows.Count;

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(SortExpression_proList)) && (!string.IsNullOrEmpty(SortDirection_proList)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", SortExpression_proList, SortDirection_proList);
            }
            // GridView绑定并显示数据
            this.gvproList.DataSource = dtBind;
            this.gvproList.DataBind();

            pageCount = gvproList.PageCount;
            pageIndex = gvproList.PageIndex;

        }
        protected void gvproList_DataBound(object sender, EventArgs e)
        {
            if (Session["Edit_CPID"] != null)
            {
                foreach (GridViewRow gvr in gvproList.Rows)
                {
                    if (((HyperLink)gvr.Cells[0].Controls[0]).Text.Equals(
                        SpecialCharHandler.HtmlDiscode(Session["Edit_CPID"].ToString())))
                    {
                        ClientScript.RegisterStartupScript(GetType(), "SelectRow", "<script>MarkRow(" + gvr.RowIndex + "); </script>");

                    }
                }
                Session["Edit_CPID"] = null;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            int isok = 0;

            bool ischecked = false;
            for (int i = 0; i <= gvproList.Rows.Count - 1; i++)
            {
                CheckBox CheckBox = (CheckBox)gvproList.Rows[i].FindControl("chkdelete");
                if (CheckBox.Checked == true)
                {
                    string proid = ((HyperLink)gvproList.Rows[i].Cells[0].Controls[0]).Text;
                    isok = proManage.Delete_Product(Convert.ToInt32(proid));
                    ischecked = true;
                }

            }
            if (ischecked)
            {
                ischecked = false;
                if (isok == 1)
                {
                    ShowMsg("删除产品信息成功！", 0);
                    ViewState["proAll"] = proManage.Get_ProductAll(drpState.SelectedItem.Text,
                                                drpIssuanceAgency.SelectedItem.Text,
                                                drpDeadline.SelectedValue,
                                                drpExpectedReturnOnTime.SelectedValue,
                                                drpTZMK.SelectedValue,
                                                drpInvestmentIndustry.SelectedItem.Text,
                                                drpClass.SelectedItem.Text,
                                                SpecialCharHandler.HtmlEncode(txt_Key.Text), "");
                    SearchproBindData();
                }
                else
                {
                    ShowMsg("删除产品信息失败！", 1);
                }
            }
        }
    }
}